package com.bright.ghj.stats.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class TestServiceImpl {

    private final JdbcTemplate jdbcTemplatePrimary;

    public void testProc() {
        System.out.println("进入testServiceImpl 测试proc类-----------");

        List<Map<String, Object>> maps = jdbcTemplatePrimary.queryForList("select * from notes");
        System.out.println(maps.size());

//        jdbcTemplatePrimary.update("call proc_test_no_return(12,'test')");
//        System.out.println("proc_test_no_return  success");

//        maps = jdbcTemplatePrimary.queryForList("call proc_test(12,'test')");
//        maps = jdbcTemplatePrimary.queryForList("DECLARE c1 integer; c2 varchar; BEGIN c1:=1; c2:='a'; SELECT c1,c2; END ");

//        List<Map<String, Object>> mapList = executeGetFirstResultSet(jdbcTemplatePrimary, "DECLARE c1 integer; c2 varchar; BEGIN c1:=1; c2:='a'; SELECT c1,c2; END ");

//        System.out.println("declare: " + mapList);

//        jdbcTemplatePrimary.queryForList("select 1,'a' from dual;");
//
//        jdbcTemplatePrimary.queryForList("BEGIN SELECT 1,'a' from dual; END ");


//        maps = jdbcTemplatePrimary.queryForList("call IsWrite('35678_010401003','01')");

//        List<Map<String, Object>> mapList2 = executeGetFirstResultSet(jdbcTemplatePrimary, "call IsWrite('35678_010401003','01');");
//        System.out.println("IsWrite: " + mapList2);


    }


//    public List<Map<String, Object>> executeGetFirstResultSet(JdbcTemplate jdbcTemplate, String sql) {
//        // 人大金仓执行语句块报错Multiple ResultSets were returned by the query 用此方法返回第一个结果集
//        return jdbcTemplate.execute(sql, (CallableStatementCallback<List<Map<String, Object>>>) cs -> {
//            List<Map<String, Object>> list = new ArrayList<>();
//            boolean execute = cs.execute();
//
//            if (execute) {
//                ResultSet resultSet = cs.getResultSet();
//                while (resultSet.next()) {
//                    ResultSetMetaData meta = resultSet.getMetaData();
//                    int colcount = meta.getColumnCount();
//                    Map<String, Object> map = new HashMap<>();
//                    for (int i = 1; i <= colcount; i++) {
//                        String name = meta.getColumnLabel(i);
//                        map.put(name, resultSet.getObject(i));
//                    }
//                    list.add(map);
//                }
////                execute = cs.getMoreResults();
//            }
//            return list;
//        });
//    }

}
